$(function () {

    function query_goods_tuijian_by_user_list() {
        api.lgc.query_goods_tuijianitem_page_by_user_list({page_id:html_data.page_id}).then(function (data) {
            // html_data.goods_order_list = data.result.list;
            html_data.good_page_index = data.result.page_index;
            html_data.good_page_count = data.result.page_count;
            _(data.result.list).each(function (item) {
                if(!html_data.page_name){
                    html_data.page_name = item.page__name;
                    html_data.user__icon_url = item.page__user__icon_url;
                    html_data.goods_img_url = baseConfig.qn_host + item.tuijian__goods__small_image__fileurl+'?imageView2/1/w/50/h/50';
                    html_data.goods_name = item.tuijian__goods__name;
                }
                html_data.good_comment_list.push(item);
                if(item.tuijian__file_type==0){

                }else if(item.tuijian__file_type==1){
                    try{
                        item.tuijian__files_json = JSON.parse(item.tuijian__files_json);
                    }catch (e) {
                        item.tuijian__files_json = [];
                    }

                }else if(item.tuijian__file_type==2){
                    try{
                        item.tuijian__video_obj = JSON.parse(item.tuijian__videos_json);
                    }catch (e) {
                        item.tuijian__video_obj = [];
                    }


                }

            });
        });
    }



    var html_data = {
        baseConfig: baseConfig,
        un_pay_order_list: [],
        goods_order_list: [],
        pay_order_list: [],
        good_comment_list: [],
        bad_comment_list: [],
        hide_comment_list: [],
        select_tuijian: [],
        select_tuijian_page: [],
        show_index:0,
        no_page_index:0,
        pay_page_index:0,
        over_page_index:0,
        no_page_count:0,
        pay_page_count:0,
        over_page_count:0,
        dispute_page_index:0,
        dispute_page_count:0,
        good_page_index:0,
        good_page_count:0,
        bad_page_index:0,
        bad_page_count:0,
        hide_page_index:0,
        hide_page_count:0,
        page_name:"",
        user__icon_url:"",
        goods_img_url:"",
        goods_name:"",
        page_id:GetQueryString("page_id"),
        shared_user_id:GetQueryString("shared_user_id"),
        show_pengyou_dongtai_images:false,
        show_share_bar:false,
        start_list:[{type:1, index:0},{type:1, index:1},{type:1, index:2},{type:1, index:3},{type:1, index:4}],
    };
    var app = new Vue({
        el: '#orderlist',
        data: html_data,
        methods: {
            price_filter: price_filter,
            page_back: function () {
                baseConfig.page_back();
            },
            query_goods_tuijian_by_user_list: query_goods_tuijian_by_user_list,
            make_goods_order: function (goods_order) {
                api.lorder.confirm_goods_received({goods_order_id: goods_order.id}).then(function (data) {
                    goods_order.goods_status = data.result.goods_status;
                    Magiclamp.mcAlert("提示", data.message);
                });
            },
            get_fengmian:function(img_url1, img_url2){
                if(img_url1){
                    return baseConfig.qn_host + img_url1+'?imageView2/1/w/270/h/270';
                }else if(img_url2){
                    return baseConfig.qn_host + img_url2+'?imageView2/1/w/270/h/270';
                }
            },
            first_name:function(realname){
                var title = realname.substring(0, 1);
                for (var i = 0; i < realname.length; i++) {
                    title += "*"
                }
                return title;
            },
            delete_tuijian:function(){

                api.lgc.del_tuijian({tuijian_ids:html_data.select_tuijian.join(",")}).then(function (value) {
                    _(html_data.good_comment_list).each(function (item) {
                        _(html_data.select_tuijian).each(function (sub) {
                            if(sub==item.id){
                                item.is_active = false;

                            }
                        })

                    });
                    html_data.select_tuijian = []
                })
            },
            delete_tuijian_page:function(){

                api.lgc.del_goods_tuijian_page({page_ids:html_data.select_tuijian_page.join(",")}).then(function (value) {
                    _(html_data.bad_comment_list).each(function (item) {
                        _(html_data.select_tuijian_page).each(function (sub) {
                            if(sub==item.id){
                                item.is_active = false;

                            }
                        })

                    });
                    html_data.select_tuijian_page = []
                })
            },
            create_page:function(){
                Magiclamp.mcPrompt("请输入推荐页面名字", "推荐页面名字", function (data) {
                    if(!data){
                        Magiclamp.mcAlert("提示","请提供页面名字");
                        return
                    }

                    api.lgc.create_goods_tuijian_page({tuijian_ids:html_data.select_tuijian.join(","), name: data}).then(function (value) {
                        Magiclamp.mcAlert("提示","推荐页面创建成功");
                        html_data.bad_comment_list = [];
                        query_goods_tuijian_page_by_user_list();
                    })
                })
            },
            go_goods_detail:function (goods_id) {
                location.href = 'goods.html?goods_id='+goods_id
            },
            go_tuijian_detail:function (goods_id) {
                // location.href = 'goods.html?goods_id='+goods_id
            },
            edit_pingjia:function (order) {
                location.href = 'goods_tuijian_update.html?goods_id='+order.goods_id
            },
            show_big_pengyou_img: function (item, fileurl) {
                // program_data.show_pengyou_dongtai = false;
                html_data.show_pengyou_dongtai_images = true;
                var width = window.innerWidth
                    || document.documentElement.clientWidth
                    || document.body.clientWidth;
                var height = window.innerHeight
                    || document.documentElement.clientHeight
                    || document.body.clientHeight;
                setTimeout(function () {
                    var pswpElement = document.getElementById("all_pengyou_pswp");

                    var items = [];
                    var index = 0;

                    _(item.tuijian__files_json).each(function (sub, i) {
                        items.push({src: baseConfig.qn_host + sub.fileurl +'?imageView2/0/w/'+width+'/h/'+(width*4/3), w: width, h: (width*4/3)})
                        if (sub.fileurl == fileurl) {
                            index = i;
                        }
                    })

                    var options = {
                        // optionName: 'option value'
                        // for example:
                        mainClass: 'pswp--minimal--dark',
                        index: index, // start at first slide

                    };

                    var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
                    gallery.listen('close', function () {
                        // program_data.show_pengyou_dongtai = true;
                        html_data.show_pengyou_dongtai_images = false;
                    });
                    gallery.init();
                }, 200);

            },
            play_video: function (dt) {
                var v = document.getElementById("video" + dt.id);
                var vc = document.getElementById("video_play" + dt.id);
                if (!dt.playing) {

                    v.play();
                    dt.playing = true;
                    vc.style.display = "none";
                } else {
                    v.pause();
                    dt.playing = false;

                    vc.style.display = "block";
                }
            },
            fenxiang_url: function(order){

                api.sys.create_sharelink({goods_id:order.goods_order_item_list[0].goods_id}).then(function (value) {
                    var url = value.result.url;
                    try {
                        wx.onMenuShareTimeline({
                            title: '华迅直购', // 分享标题
                            link: url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                            imgUrl: 'http://www.mmggoo.com/static/weixin/img2/icon.png', // 分享图标
                            success: function (res) {
                                // 用户确认分享后执行的回调函数
                                console.log(res);
                            },
                            cancel: function (res) {
                                // 用户取消分享后执行的回调函数
                                console.log(res);
                            }
                        });
                        wx.onMenuShareAppMessage({
                            title: "华迅直购", // 分享标题
                            desc: "华迅直购", // 分享描述
                            link: url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                            imgUrl: 'http://www.mmggoo.com/static/weixin/img2/icon.png', // 分享图标
                            type: '', // 分享类型,music、video或link，不填默认为link
                            dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
                            success: function (res) {
                                // 用户确认分享后执行的回调函数
                                console.log(res);
                            },
                            cancel: function (res) {
                                // 用户取消分享后执行的回调函数
                                console.log(res);
                            }
                        });
                        Magiclamp.mcAlert("提示", "请点击右上角菜单进行分享");
                    } catch (res) {
                        console.log(res);
                    }
                })
            },

            shengyudays: function (goods_order) {
                if (goods_order.goods_status == 4 || goods_order.goods_status == 0 || goods_order.goods_status == 1) {
                    return "";
                }
                if (goods_order.dispute_step == 0) {
                    //"received_date" "shipped_date"
                    if (goods_order.goods_status == 2) {
                        var shipped_date = moment();
                        var end_date = moment(goods_order.shipped_date);
                        end_date.add(7, 'days');
                        var days = end_date.diff(shipped_date, 'days');
                        if (days == 0) {
                            return "(剩余：" + end_date.diff(shipped_date, 'hours') + "小时)"
                        } else {
                            return "(剩余：" + days + "天)";
                        }
                    } else if (goods_order.goods_status == 3) {
                        var shipped_date = moment();
                        var end_date = moment(goods_order.received_date);
                        end_date.add(7, 'days');
                        var days = end_date.diff(shipped_date, 'days');
                        if (days == 0) {
                            return "(剩余：" + end_date.diff(shipped_date, 'days') + "小时)"
                        } else {
                            return "(剩余：" + days + "天)";
                        }
                    }

                } else {
                    return "";
                }
            },

        }
    });

    html_data.sessionid = GetQueryString('sessionid');
    if (html_data.sessionid) {
        html_data.sessionid_q = "sessionid=" + html_data.sessionid;
        client_storage('sessionid', html_data.sessionid);
    }

    document.getElementsByTagName("BODY")[0].onpageshow = function (event) {
        if(event && !event.persisted){
            return;
        }
        html_data.un_pay_order_list = [];
        html_data.goods_order_list = [];
        html_data.pay_order_list = [];
        html_data.dispute_order_list = [];

        login_mogu(function () {
            // api.sys.get_userinfo().then(function (data) {
            //     me_data.user = data.result
            // });


            // api.lgoods.query_cart_list().then(function (data) {
            //     cart_data.cart_list = data.result.list
            //
            //     // tab_cart.checkAll();
            //     cart_data.goods_ids=[];
            //     cart_data.cart_list.forEach(function (item) {
            //         if(item.is_active){
            //             cart_data.goods_ids.push(item.goods_id);
            //         }
            //     });
            //     cart_data.is_all = true;
            // });
            // query_un_pay_order_info_list();
            // query_pay_order_info_list();
            html_data.good_comment_list = [];
            html_data.bad_comment_list = [];
            html_data.hide_comment_list = [];
            query_goods_tuijian_by_user_list();

            api.sys.get_userinfo().then(function (data) {
                if(data.result.id.toString()==html_data.shared_user_id){

                    var flag = client_storage("go_tuijian_detail");
                    if(!!flag){
                        html_data.show_share_bar = true;
                    }
                    client_storage("go_tuijian_detail", "");

                }else{
                    api.sys.set_share_user_by_user({from_user_id: html_data.shared_user_id}).then(function (value) {
                    })
                }
            });

        }, "", null, true);
    };
    document.getElementsByTagName("BODY")[0].onpageshow();



    api.sys.wx_js_sdk_info({url:location.href}).then(function (data) {

        wx.config({
            debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
            appId: 'wxd78bb63115a11db4', // 必填，公众号的唯一标识
            timestamp: data.result.timestamp, // 必填，生成签名的时间戳
            nonceStr: data.result.noncestr, // 必填，生成签名的随机串
            signature: data.result.signature,// 必填，签名
            jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage'] // 必填，需要使用的JS接口列表
        });
        wx.error(function (res) {
            // config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
            console.log(res);
        });

        wx.ready(function () {

            wx.onMenuShareTimeline({
                title: html_data.page_name, // 分享标题
                link: location.href, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                imgUrl: html_data.goods_img_url, // 分享图标
                success: function (res) {
                    // 用户确认分享后执行的回调函数

                    console.log(res);
                },
                cancel: function (res) {
                    // 用户取消分享后执行的回调函数
                    console.log(res);
                }
            });
            wx.onMenuShareAppMessage({
                title: html_data.page_name, // 分享标题
                desc: html_data.goods_name, // 分享描述
                link: location.href, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                imgUrl: html_data.goods_img_url, // 分享图标
                type: '', // 分享类型,music、video或link，不填默认为link
                dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
                success: function (res) {
                    // 用户确认分享后执行的回调函数
                    console.log(res);
                },
                cancel: function (res) {
                    // 用户取消分享后执行的回调函数
                    console.log(res);
                }
            });


        });


    })

});
